<template>
  <div>
    <el-form :model="userInfo" :rules="rules">
      <el-form-item label="用户名/手机号" prop="mobile">
        <el-input v-model="userInfo.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userInfo.password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="passwordAgain">
        <el-input v-model="userInfo.passwordAgain" />
      </el-form-item>
      <el-button>注册</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      // value是当前的密码值，和之前的密码对比，如果全等的话就赋值给isSame变量
      const isSame = value === this.userInfo.password
      if (isSame) {
        // 成功通过
        callback()
      } else {
        // 验证失败 未通过
        callback(new Error('两次密码不一致'))
      }
    }
    return {
      userInfo: {
        mobile: '',
        password: '',
        passwordAgain: ''
      },
      rules: {
        mobile: [
          { required: true, message: '用户名/手机号不得为空', trigger: 'blur' },
          { pattern: /^1[3-9][0-9]{9}$/, message: '手机号/用户名不合法', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不得为空', trigger: 'blur' },
          { min: 3, max: 16, message: '密码长度在3-16个字符', trigger: 'blur' }
        ],
        passwordAgain: [
          { validator: validatePass, required: true, trigger: 'blur' }
        ]
      }
    }
  }
}
</script>

<style>

</style>
